package com.onepice.mlp.core.serviceImpl;

import com.onepice.mlp.core.service.UserService;
import com.onepice.mlp.data.dao.UserDao;
import com.onepice.mlp.data.entity.User;
import com.onepice.mlp.utils.commom.ConvUtil;
import com.onepice.mlp.utils.commom.PageUtil;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author FireWang
 * @date 2022/07/11 18:26:57
 */
@Service
@AllArgsConstructor
@Transactional
public class UserServiceImpl implements UserService {
    private UserDao userDao;

    @Override
    public List<User> getAll() {
        return userDao.findAll();
    }

    @Override
    public User getById(Integer id) {
        Optional<User> user = userDao.findById(id);
        return user.get();
    }

    @Override
    public void insert(User user) {
        user.setCreateTime(new Date());
        userDao.save(user);
    }

    @Override
    public void update(User user) {
        user.setModifyTime(new Date());
        userDao.save(user);
    }

    @Override
    public void deleteById(Integer id) {
        userDao.deleteById(id);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userDao.findByPhone(phone);
    }

    @Override
    public User getUserByUerName(String userName) {
        return userDao.findByUserName(userName);
    }

    @Override
    public Page<User> getUserForPage(PageUtil page) {
        //查询规格定义
        Specification<User> specification = new Specification<User>() {

            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                String userName = ConvUtil.convToString(page.getParamMap().get("userName"));
                String phone = ConvUtil.convToString(page.getParamMap().get("phone"));

                if(!"".equals(userName)){
                    predicates.add(cb.like(root.get("userName").as(String.class),  "%" + userName + "%"));
                }
                if(!"".equals(phone)){
                    predicates.add(cb.like(root.get("phone").as(String.class),"%" + phone + "%"));
                }
                if(page.getParamMap().get("status") != null){
                    predicates.add(cb.equal(root.get("status").as(Integer.class), page.getParamMap().get("status")));
                }

                //默认不查管理员
                predicates.add(cb.notEqual(root.get("userType").as(String.class), 1));

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        //分页查询
        return userDao.findAll(specification, page.getPageable());
    }
}
